package kr.blogspot.stiner01.moduletutorial;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class GyroscopeTest extends Activity implements SensorEventListener
{
    StringBuilder _builder = new StringBuilder();
    TextView _textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        _textView = new TextView(this);
        
        setContentView(_textView);
        
        SensorManager sensorMgr = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        
        if (sensorMgr.getSensorList(Sensor.TYPE_ORIENTATION).size() == 0)
        {
            _textView.setText("No accelerometer installed");
        }
        else
        {
            Sensor orientationSensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ORIENTATION);
            if (!sensorMgr.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_GAME))
            {
                _textView.setText("Couldn't register sensor listener");
            }
        }
    }
    
    @Override
    public void onSensorChanged(SensorEvent event)
    {
        _builder.setLength(0);
        _builder.append("x: ");
        _builder.append(String.format("%1.1f", event.values[0]));
        _builder.append(", y: ");
        _builder.append(String.format("%1.1f", event.values[1]));
        _builder.append(", z: ");
        _builder.append(String.format("%1.1f", event.values[2]));
        
        _textView.setText(_builder.toString());
    }
    
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
        
    }
}
